package com.rongxun.hbwallet.ui.activities;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongxun.base.MvpActivity;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.model.bean.ImageCode;
import com.rongxun.hbwallet.mvp.contract.IForget;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.hbwallet.mvp.presenter.ForgetPresenter;
import com.rongxun.utils.AppUtils;
import com.rongxun.utils.MyToast;
import com.rongxun.utils.StatusBarUtil;
import com.rongxun.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/17 14:20
 * 描述	      忘记密码

 */
public class ForgetPasswordActivity extends MvpActivity<IForget.IView,ForgetPresenter> implements IForget.IView   {
    @BindView(R.id.toolbar_title)
    TextView  mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar   mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    private Dialog mLoadingDialog;
    @BindView(R.id.forget_phone)
    EditText  et_phone;
    @BindView(R.id.forget_ed_imgcode)
    EditText  et_img_code;
    @BindView(R.id.forget__mobile_code)
    EditText  et_forget_mobile_code;
    @BindView(R.id.forget_password)
    EditText  et_forget_password;
    @BindView(R.id.forget_tow_password)
    EditText  et_forget_tow_password;
    @BindView(R.id.forget_img_code)
    ImageView iv_forget_img_code;
    private String captchaId;
    @Override
    protected void init() {
        StatusBarUtil.setToolBarPaddingTop(mToolbar, this);
        mToolbarTitle.setText(getString(R.string.forget_password_));
        mToolbarBack.setVisibility(View.VISIBLE);
        mLoadingDialog = LoadingDialog.createLoadingDialog(this, "加载中", false);
    }
    @Override
    protected int getContentView() {
        return R.layout.activity_forget;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading(boolean show) {
        if (show) {
            mLoadingDialog.show();
        } else {
            mLoadingDialog.hide();
        }
    }

    @Override
    public void showError(String msg, String errorCode) {
        MyToast.showToast(this,msg);
    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }

    @Override
    public void release() {
        mLoadingDialog.cancel();
        mLoadingDialog.dismiss();
    }
    @OnClick({R.id.but_forget,R.id.forget_img_code,R.id.forget_get_code,R.id.toolbar_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.forget_img_code:
                getPresenter().getImageCode(et_phone.getText().toString());
                break;
            case R.id.toolbar_back:
                onBackPressed();
                break;
            case R.id.forget_get_code:
                String country=getContext().getString(R.string.current_country);
                getPresenter().getMobileNumber(et_img_code.getText().toString(),captchaId,country,et_phone.getText().toString());
                break;
            case R.id.but_forget:
                getPresenter().findPassword(et_forget_mobile_code.getText().toString(),"captcha",et_forget_password.getText().toString(),et_forget_tow_password.getText().toString());
                break;

        }
    }

    @NonNull
    @Override
    public ForgetPresenter createPresenter() {
        return new ForgetPresenter(this,new ApiModel(this));
    }

    @Override
    public void showImageView(ImageCode code) {
        if(code!=null){
            captchaId=code.getCaptcha().getId();
            iv_forget_img_code.setImageBitmap(AppUtils.stringtoBitmap(code.getCaptcha().getCode()));
        }
    }
}
